home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / mpeg / mpgplyr1.lha / src / hybrid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-09  |  6.6 KB  |  232 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /* This file contains C code to implement an ordered dither. */
  22.  
  23. #include "video.h"
  24. #include "dither.h"
  25.  
  26. #define DITH_SIZE 16
  27.  
  28.  
  29. /* Structures used to implement hybrid ordered dither/floyd-steinberg
  30.    dither algorithm.
  31. */
  32.  
  33. static unsigned char *l_darrays[DITH_SIZE];
  34. static unsigned char cr_fsarray[256][4];
  35. static unsigned char cb_fsarray[256][4];
  36.  
  37.  
  38. /*
  39.  *--------------------------------------------------------------
  40.  *
  41.  *  InitHybridDither--
  42.  *
  43.  *    Structures intialized for hybrid dithering. Ordered dither
  44.  *      patterns set for luminance channel, f-s errors precomputed
  45.  *      for chrominance channels.
  46.  *
  47.  * Results:
  48.  *    None.
  49.  *
  50.  * Side effects:
  51.  *      None.
  52.  *
  53.  *--------------------------------------------------------------
  54.  */
  55.  
  56. void
  57. InitHybridDither()
  58. {
  59.   int i, j, k, err_range, threshval;
  60.   unsigned char *lmark;
  61.  
  62.   for (i=0; i<DITH_SIZE; i++) {
  63.     lmark = l_darrays[i] = (unsigned char *) malloc(256);
  64.  
  65.     for (j=0; j<lum_values[0]; j++) {
  66.       *lmark++ = 0;
  67.     }
  68.  
  69.     for (j=0; j<(LUM_RANGE-1); j++) {
  70.       err_range = lum_values[j+1] - lum_values[j];
  71.       threshval = ((i * err_range) / DITH_SIZE)+lum_values[j];
  72.  
  73.       for (k=lum_values[j]; k<lum_values[j+1]; k++) {
  74.     if (k > threshval) *lmark++ = ((j+1) * (CR_RANGE * CB_RANGE));
  75.     else *lmark++ = (j * (CR_RANGE * CB_RANGE));
  76.       }
  77.     }
  78.  
  79.     for (j=lum_values[LUM_RANGE-1]; j<256; j++) {
  80.       *lmark++ = (LUM_RANGE-1)*(CR_RANGE * CB_RANGE);
  81.     }
  82.  
  83.   }
  84.   {
  85.     int cr1, cr2, cr3, cr4, err1, err2;
  86.     int cb1, cb2, cb3, cb4, val, nval;
  87.  
  88.     for (i=0; i<256; i++) {
  89.  
  90.       val = i;
  91.  
  92.       cr1 = (val * CR_RANGE) / 256;
  93.       err1 = (val - cr_values[cr1])/2;
  94.       err2 = (val - cr_values[cr1]) - err1;
  95.  
  96.       nval = val+err1;
  97.       if (nval > 255) nval = 255;
  98.       else if (nval < 0) nval = 0;
  99.       cr2 = (nval * CR_RANGE) / 256;
  100.       err1 = (nval - cr_values[cr2])/2;
  101.  
  102.       nval = val+err2;
  103.       if (nval > 255) nval = 255;
  104.       else if (nval < 0) nval = 0;
  105.       cr3 = (nval * CR_RANGE) / 256;
  106.       err2 = (nval - cr_values[cr3])/2;
  107.  
  108.       nval = val+err1+err2;
  109.       if (nval > 255) nval = 255;
  110.       else if (nval < 0) nval = 0;
  111.       cr4 = (nval * CR_RANGE) / 256;
  112.  
  113.       cr_fsarray[i][0] = cr1*CB_RANGE;
  114.       cr_fsarray[i][1] = cr2*CB_RANGE;
  115.       cr_fsarray[i][2] = cr3*CB_RANGE;
  116.       cr_fsarray[i][3] = cr4*CB_RANGE;
  117.     }
  118.     
  119.     for (i=0; i<256; i++) {
  120.  
  121.       val = i;
  122.  
  123.       cb1 = (val * CB_RANGE) / 256;
  124.       err1 = (val - cb_values[cb1])/2;
  125.       err2 = (val - cb_values[cb1]) - err1;
  126.  
  127.       nval = val+err1;
  128.       if (nval > 255) nval = 255;
  129.       else if (nval < 0) nval = 0;
  130.       cb2 = (nval * CB_RANGE) / 256;
  131.       err1 = (nval - cb_values[cb2])/2;
  132.  
  133.       nval = val+err2;
  134.       if (nval > 255) nval = 255;
  135.       else if (nval < 0) nval = 0;
  136.       cb3 = (nval * CB_RANGE) / 256;
  137.       err2 = (nval - cb_values[cb3])/2;
  138.  
  139.       nval = val+err1+err2;
  140.       if (nval > 255) nval = 255;
  141.       else if (nval < 0) nval = 0;
  142.       cb4 = (nval * CB_RANGE) / 256;
  143.  
  144.       cb_fsarray[i][0] = cb1;
  145.       cb_fsarray[i][1] = cb2;
  146.       cb_fsarray[i][2] = cb3;
  147.       cb_fsarray[i][3] = cb4;
  148.     }
  149.   }
  150. }
  151.  
  152. /*
  153.  *--------------------------------------------------------------
  154.  *
  155.  * HybridDitherImage --
  156.  *
  157.  *    Dithers an image using an ordered dither.
  158.  *    Assumptions made:
  159.  *      1) The color space is allocated y:cr:cb = 8:4:4
  160.  *      2) The spatial resolution of y:cr:cb is 4:1:1
  161.  *      The luminance channel is dithered based on the standard
  162.  *      ordered dither pattern for a 4x4 area. The Chrominance 
  163.  *      channels are dithered based on precomputed f-s errors.
  164.  *      Two errors are propogated per pixel. Errors are NOT propogated
  165.  *      beyond a 2x2 pixel area. 
  166.  *
  167.  * Results:
  168.  *    None.
  169.  *
  170.  * Side effects:
  171.  *    None.
  172.  *
  173.  *--------------------------------------------------------------
  174.  */
  175. void
  176. HybridDitherImage (lum, cr, cb, out, h, w)
  177.     unsigned char *lum;
  178.     unsigned char *cr;
  179.     unsigned char *cb;
  180.     unsigned char *out;
  181.     int w, h;
  182. {
  183.   unsigned char *l, *r, *b, *o1, *o2;
  184.   unsigned char *l2;
  185.   int i, j;
  186.  
  187.   l = lum;
  188.   l2 = lum+w;
  189.   r = cr;
  190.   b = cb;
  191.   o1 = out;
  192.   o2 = out+w;
  193.  
  194.   for (i=0; i<h; i+=4) {
  195.  
  196.     for (j=0; j<w; j+=4) {
  197.  
  198.       *o1++ = pixel[(l_darrays[0][*l++] | cr_fsarray[*r][0] | cb_fsarray[*b][0])];
  199.       *o1++ = pixel[(l_darrays[8][*l++] | cr_fsarray[*r][1] | cb_fsarray[*b][1])];
  200.       *o2++ = pixel[(l_darrays[12][*l2++] | cr_fsarray[*r][2] | cb_fsarray[*b][2])];
  201.       *o2++ = pixel[(l_darrays[4][*l2++] | cr_fsarray[*r++][3] | cb_fsarray[*b++][3])];
  202.  
  203.       *o1++ = pixel[(l_darrays[2][*l++] | cr_fsarray[*r][0] | cb_fsarray[*b][0])];
  204.       *o1++ = pixel[(l_darrays[10][*l++] | cr_fsarray[*r][1] | cb_fsarray[*b][1])];
  205.       *o2++ = pixel[(l_darrays[14][*l2++] | cr_fsarray[*r][2] | cb_fsarray[*b][2])];
  206.       *o2++ = pixel[(l_darrays[6][*l2++] | cr_fsarray[*r++][3] | cb_fsarray[*b++][3])];
  207.     }
  208.  
  209.     l += w; l2 += w;
  210.     o1 += w; o2 += w;
  211.  
  212.     for (j=0; j<w; j+=4) {
  213.  
  214.       *o1++ = pixel[(l_darrays[3][*l++] | cr_fsarray[*r][1] | cb_fsarray[*b][1])];
  215.       *o1++ = pixel[(l_darrays[11][*l++] | cr_fsarray[*r][0] | cb_fsarray[*b][0])];
  216.       *o2++ = pixel[(l_darrays[15][*l2++] | cr_fsarray[*r][3] | cb_fsarray[*b][3])];
  217.       *o2++ = pixel[(l_darrays[7][*l2++] | cr_fsarray[*r++][2] | cb_fsarray[*b++][2])];
  218.  
  219.       *o1++ = pixel[(l_darrays[1][*l++] | cr_fsarray[*r][1] | cb_fsarray[*b][1])];
  220.       *o1++ = pixel[(l_darrays[9][*l++] | cr_fsarray[*r][0] | cb_fsarray[*b][0])];
  221.       *o2++ = pixel[(l_darrays[13][*l2++] | cr_fsarray[*r][3] | cb_fsarray[*b][3])];
  222.       *o2++ = pixel[(l_darrays[5][*l2++] | cr_fsarray[*r++][2] | cb_fsarray[*b++][2])];
  223.     }
  224.  
  225.     l += w; l2 += w;
  226.     o1 += w; o2 += w;
  227.   }
  228. }
  229.  
  230.  
  231.   
  232.